<template>
  <div class="sku">
    <el-table border :data="skuArr">
      <el-table-column label="序号" type="index" width="80px"></el-table-column>
      <el-table-column
        label="名称"
        show-overflow-tooltip
        width="150px"
        prop="skuName"
      ></el-table-column>
      <el-table-column
        label="描述"
        type="index"
        show-overflow-tooltip
        width="150px"
        prop="skuDesc"
      ></el-table-column>
      <el-table-column label="默认图片" type="index" width="150px">
        <template #="{ row, $index }">
          <img :src="row.skuDefaultImg" alt="" style="height: 100px" />
        </template>
      </el-table-column>
      <el-table-column
        label="重量（g）"
        width="150px"
        prop="weight"
      ></el-table-column>
      <el-table-column
        label="价格（元）"
        width="150px"
        prop="price"
      ></el-table-column>
      <el-table-column label="操作" fixed="right" min-width="250px">
        <template #="{ row, $index }">
          <el-button
            type="primary"
            size="small"
            :icon="row.isSale == 1 ? 'Bottom' : 'Top'"
            @click="updateSale(row)"
          ></el-button>
          <el-button
            type="primary"
            size="small"
            icon="Edit"
            @click="editSku(row)"
          ></el-button>
          <el-button
            type="primary"
            size="small"
            icon="InfoFilled"
            @click="lookSkuInfo(row)"
          ></el-button>
          <el-popconfirm
            :title="`你确定要删除${row.skuName}?`"
            width="200px"
            @confirm="deleteSku(row)"
          >
            <template #reference>
              <el-button type="danger" size="small" icon="Delete"></el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      style="margin-top: 20px"
      v-model:current-page="pageNo"
      v-model:page-size="pageSize"
      :page-sizes="[10, 20, 30, 40]"
      size
      background
      layout="total, sizes, prev, pager, next, jumper"
      :total="skuTotal"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    <!-- 抽屉组件，展示当前sku的详情 -->
    <el-drawer v-model="drawerShow" direction="rtl" size="40%">
      <template #header>
        <h4>查看商品的详情信息</h4>
      </template>
      <template #default>
        <el-row style="margin: 10px 0px">
          <el-col :span="6">名称</el-col>
          <el-col :span="18">{{ skuInfo.skuName }}</el-col>
        </el-row>
        <el-row style="margin: 10px 0px">
          <el-col :span="6">描述</el-col>
          <el-col :span="18">{{ skuInfo.skuDesc }}</el-col>
        </el-row>
        <el-row style="margin: 10px 0px">
          <el-col :span="6">价格</el-col>
          <el-col :span="18">{{ skuInfo.price }}</el-col>
        </el-row>
        <el-row
          v-if="skuInfo.skuAttrValueList.length > 0"
          style="margin: 10px 0px"
        >
          <el-col :span="6">平台属性</el-col>
          <el-col :span="18">
            <el-tag
              v-for="item in skuInfo.skuAttrValueList"
              :key="item.id"
              style="margin: 5px"
              >{{ item.valueName }}</el-tag
            >
          </el-col>
        </el-row>
        <el-row
          v-if="skuInfo.skuSaleAttrValueList.length > 0"
          style="margin: 10px 0px"
        >
          <el-col :span="6">销售属性</el-col>
          <el-col :span="18">
            <el-tag
              v-for="item in skuInfo.skuSaleAttrValueList"
              :key="item.id"
              style="margin: 5px"
              >{{ item.saleAttrValueName }}</el-tag
            >
          </el-col>
        </el-row>
        <el-row v-if="skuInfo.skuImageList.length > 0" style="margin: 10px 0px">
          <el-col :span="6">商品图片</el-col>
          <el-col :span="18">
            <el-carousel
              :interval="4000"
              type="card"
              height="200px"
              indicator-position="none"
              arrow="never"
            >
              <el-carousel-item
                v-for="item in skuInfo.skuImageList"
                :key="item.id"
              >
                <img
                  :src="item.imgUrl"
                  alt=""
                  style="width: 100%; height: 100%"
                />
              </el-carousel-item>
            </el-carousel>
          </el-col>
        </el-row>
      </template>
    </el-drawer>
  </div>
</template>
<script setup lang="ts">
import { onBeforeMount, onMounted, reactive, ref } from 'vue'
import {
  getSKUList,
  getSKUInfo,
  deleteSKU,
  cancelSaleAPI,
  saleSkuAPI,
} from '@/api/product/sku/index.ts'
import { ElMessage } from 'element-plus'

// sku列表页数据
let skuArr = ref([])
// sku列表页总数据条数
let skuTotal = ref(0)
// 分页每页的条数
let pageSize = ref(10)
// 分页当前的页数
let pageNo = ref(1)
// 抽屉是否展示
let drawerShow = ref<boolean>(false)
// 商品详情对象
let skuInfo = ref<any>({})
// 初始化获取sku列表页数据
const getSkulist = async (pageNo: number, pageSize: number) => {
  let result = await getSKUList(pageNo, pageSize)
  const { code, data } = result
  if (code == 200) {
    console.log(data)
    skuArr.value = data.records
    skuTotal.value = data.total
  }
}
// 商品的上架与下架操作
const updateSale = async (row: any) => {
  // 如果当前商品的状态isSale是1，那么表明当前商品处于上架状态，那么更新该状态为下架状态
  if (row.isSale == 1) {
    await cancelSaleAPI(row.id)
    // 提示信息下架成功
    ElMessage.success('下架成功')
  } else {
    // 如果当前状态是下架状态，那么将其状态改为上架状态
    await saleSkuAPI(row.id)
    ElMessage.success('上架成功')
  }
  // 更新当前列表页数据
  getSkulist(pageNo.value, pageSize.value)
}

// 编辑sku相关信息
const editSku = (row: any) => {
  ElMessage.success('程序员正在研发中...')
}
// 查看当前sku商品的详情
const lookSkuInfo = async (row: any) => {
  // 调用接口获取sku商品详情
  let result = await getSKUInfo(row.id)
  const { code, data } = result
  if (code == 200) {
    skuInfo.value = data
  }
  drawerShow.value = true
}
// 删除某项sku信息
const deleteSku = async (row: any) => {
  console.log(row)
  let result = await deleteSKU(row.id)
  const { code } = result
  if (code == 200) {
    ElMessage.success('删除成功')
    // 重新请求列表页数据
    getSkulist(pageNo.value, pageSize.value)
  } else {
    ElMessage.error('删除失败')
  }
}

// 分页改变每页展示数量
const handleSizeChange = () => {
  getSkulist(pageNo.value, pageSize.value)
}

// 翻页
const handleCurrentChange = () => {
  getSkulist(pageNo.value, pageSize.value)
}

onMounted(() => {
  getSkulist(pageNo.value, pageSize.value)
})
</script>
<style lang="scss" scoped></style>
